
layui.define(function(exports){
	var $ = layui.$
		,setter = layui.setter;

	var obj = {
		reSizeMeIndex: function (zhi,classDesc) { // 调整屏幕大小
			var z = 0;
			if(!isNaN(Number(zhi))){
				z = Number(zhi);
			}
			var winHeight = window.innerHeight;
			$('.'+classDesc+'').css("height", winHeight-z-23);
			z = null;
		}
		,postUrl:function (params, url) {//调整页面及导出可以用此方法
			var form = document.createElement("form");
			form.style.display = 'none';
			form.action = url;
			form.method = "post";
			document.body.appendChild(form);

			for(var key in params){
				var input = document.createElement("input");
				input.type = "hidden";
				input.name = key;
				input.value = params[key];
				form.appendChild(input);
			}
			form.submit();
			form.remove();
		}
		,JSON_IndexOf:function(source,val) { //获取出现JSONArr中的位置
			for (var i = 0; i < source.length; i++) {
				if (source[i] == val)
					return i;
			}
			return -1;
		}
		,JSON_Remove : function(source,val) { // 除掉JSONArr中此元素
			var index = source.indexOf(val);
			if (index > -1) {
				source.splice(index, 1);
			}
		}
		,PxConvertToNum:function(val){
			var num = val.replace(/\s+|px/gi,"");
			var zhi = 0;
			if(!isNaN(Number(num))){
				zhi = Number(num);
			}
			return zhi;
		}
		,Repead:function(name,arr){
			var result = '';
			var result = RepeadAgain(name,arr,result);
			return result;
		}
		,changeRole:function(roles,currentRole,callback,parms){
			if(roles == undefined){
				$.ajax({
					url: setter.host+'/adminmanager/role/getRoles'
					,type:'get'
					,dataType:'json'
					,success: function(resAgin){
						//请求成功后
						if(resAgin.status == setter.response.statusCode.status_More_Role){
							roles = resAgin.data.roles;
							currentRole = resAgin.data.currentRole;
							toChangeRole(roles,currentRole,callback,parms);
						}else{
							layer.msg(resAgin.msg);
						}
					}
				});
			}else{
				toChangeRole(roles,currentRole,callback,parms);
			}

		}
	};
	function RepeadAgain(name,arr,result){
		layui.each(arr,function(inx,val){
			if(val.name == name){
				result = val.value;
			}else{
				result = RepeadAgain(name,val.children,result)
			}
		})
		return result;
	}
	function toChangeRole(roles,currentRole,callback,parms){
		layer.open({
			type: 2
			,title: '切换角色'
			,content: setter.host+'/adminmanager/role/toChange'
			,area: ['620px', '220px']
			,btn: ['确定', '取消']
			,success: function(roleFrame,index){
				var subBody = roleFrame.find('iframe').contents()
				layui.each(roles,function(inx,val){
					if (inx == 0 && currentRole ==undefined ) {
						subBody.find("#roleList").append("<input type=\"radio\" checked=true  name=\"roleId\" title='" + val.name + "' value='" + val.roleId + "'/>")
					}else if(currentRole !=undefined && currentRole == val.roleId){
						subBody.find("#roleList").append("<input type=\"radio\" checked=true  name=\"roleId\" title='" + val.name + "' value='" + val.roleId + "'/>")
					}else{
						subBody.find("#roleList").append("<input type=\"radio\" name=\"roleId\"  title='" + val.name + "' value='" + val.roleId + "'/>")
					}
				})
			}
			,yes:function(index, layero){
				var iframeWindow = window['layui-layer-iframe'+ index]
					,submitID = 'LAY-role-change-submit'
					,submit = layero.find('iframe').contents().find('#'+ submitID);
				//监听提交
				iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
					var loadChangeRoleIndex = layer.msg('切换角色中', {
						icon: 16
						,shade: 0.01
					});
					$.ajax({
						url: setter.host+'/adminmanager/role/changeRole'
						,type:'post'
						,dataType:'json'
						,contentType: "application/json; charset=utf-8"
						,data:JSON.stringify(data.field)
						,success: function(resAgin){
							console.log(resAgin)
							//请求成功后
							layer.close(index);
							if(resAgin.status == setter.response.statusCode.status_Success){
								layer.close(loadChangeRoleIndex);
								callback(parms,resAgin.loginUser);
							}else{
								layer.msg(resAgin.msg);
							}
						}
					});
				});
				submit.trigger('click');
			}
		});
	}
	//对外暴露的接口
	exports('common', obj);
});